package com.google.appengine.a4ieve.servlets.resources;

import com.google.appengine.a4ieve.Const;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import org.json.JSONObject;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

class RemoveEntityServant implements Servant {

    private static final String KEY = "s_key";

    @Override
    public void process(HttpServletRequest req, HttpServletResponse resp){
        DatastoreService service = DatastoreServiceFactory.getDatastoreService();
        String key = req.getParameter(KEY);
        if (key == null) { //look for it in json
            JSONObject request = (JSONObject)req.getAttribute(Const.INNER_JSON);
            key = (String)request.get(KEY);
        }
        if (key != null) { //means we delete
            Key toDelete = KeyFactory.stringToKey(key);
            service.delete(toDelete);
        }
    }
}
